async function get_task(){
    //创建一个列表，用于同时存储已完成和未完成的任务
    let task_list = [];

    //获取已完成任务的列表
    const complete_response = await fetch('/api/complete_task', {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json'
        }
    });
    const complete_data = await complete_response.json();
    console.log("已完成任务列表:", complete_data);


    //获取未完成任务的列表
    const uncomplete_response = await fetch('/api/uncomplete_task', {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json'
        }
    });
    const uncomplete_data = await uncomplete_response.json();
    console.log("未完成任务列表:", uncomplete_data);

     // 直接返回两个列表的数组
    return [uncomplete_data, complete_data];

}

async function add_task(new_task_content, new_task_deadline){
    console.log("预计传递参数：" + JSON.stringify({
            content: new_task_content,
            deadline: new_task_deadline
        }));
    
    const response = await fetch('/api/add_task', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            content: new_task_content,
            deadline: new_task_deadline
        })
    });
    if (!response.ok) {
        throw new Error('Network response was not ok');
    }
    return await response.json();
}

async function del_task(task_id, task_status){
    console.log("删除任务:", task_id, "状态:", task_status);
    console.log("预计发送参数：" + JSON.stringify({
            task_id: task_id
            , task_status: task_status
        }));
    


    const response = await fetch('/api/del_task', {

        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            task_id: task_id
            , task_status: task_status
        })
    });
    console.log("删除任务返回数据:", response);
    
    if (!response.ok) {
        throw new Error('Network response was not ok');
    }
    return await response.json();
}

async function complete_task(task_id){
    console.log("完成任务:", task_id);
    console.log("预计发送参数：" + JSON.stringify({
            task_id: task_id
        }));
    const response = await fetch('/api/complete_task', {
        method: 'PATCH',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            task_id: task_id
        })
    });
    if (!response.ok) {
        throw new Error('Network response was not ok');
    }
    return await response.json();
}













const app = Vue.createApp({
    data() {
        return {
            uncompleted_task: [],
            completed_task: [],
            new_task_content: "",
            new_task_deadline: ""
        }
    },
    methods: {
        async get_task_func(){
            try{
                const [uncompleted_data, completed_data] = await get_task();
                console.log("任务获取成功，返回数据：" + uncompleted_data + completed_data);
                
                this.uncompleted_task = uncompleted_data;
                console.log("未完成任务列表:", this.uncompleted_task);
                
                this.completed_task = completed_data;
                console.log("已完成任务列表:", this.completed_task);
            } catch (error) {
                console.error("任务获取失败：" + error);
            }
        },
        async add_task_func(){
            try {
                console.log("添加任务：", this.new_task_content, "，任务期限：", this.new_task_deadline);
                if (this.new_task_content == "" || this.new_task_deadline == ""){
                    alert("请填写完整任务信息");
                    return;
                }

                const return_data = await add_task(this.new_task_content, this.new_task_deadline);
                console.log("任务添加成功，返回数据：" + return_data);
                this.get_task_func(); // 刷新任务列表
                this.new_task_content = ""; // 清空输入框
                this.new_task_deadline = ""; // 清空输入框
            } catch (error) {
                console.error("任务添加失败：" + error);
                this.new_task_content = ""; // 清空输入框
                this.new_task_deadline = ""; // 清空输入框
            }
        },
        async del_task_func(task_id, task_status){
            console.log("删除任务:", task_id, "状态:", task_status);
            
            try {
                const return_data = await del_task(task_id, task_status)
                console.log("任务删除成功，返回数据：" + return_data);
                this.get_task_func(); // 刷新任务列表
            } catch (error) {
                console.error("任务删除失败：" + error);
            }
        },
        async complete_task_func(task_id){
            try {
                const return_data = await complete_task(task_id)
                console.log("任务完成成功，返回数据：" + return_data);
                this.get_task_func(); // 刷新任务列表
            } catch (error) {
                console.error("任务完成失败：" + error);
            }
        }  
},
    mounted() {
        this.get_task_func();
    }
}).mount('#app');